"""
作者: 冯中华
页面: 生成订单
"""
from Common.Base import Base, open_browser

shopping_url = 'http://172.16.11.202:8080/ecshop/goods.php?id=72'


class CheckoutPage(Base):
    """封装表现层: 制作定位器过程"""
    buy_submit_loc = ('class name', 'td1')  # 立即购买按钮
    settlement_submit_loc = ('css selector', 'div.flowBox table tbody tr td:nth-of-type(2) a')  # 去结算按钮
    username_loc = ('id', 'username')  # 用户名输入框
    password_loc = ('name', 'password')  # 密码输入框
    login_submit_loc = ('name', 'login')  # 登录按钮
    payment_loc = ('name', 'payment')  # 支付方式选择银行卡汇款/转账
    order_submit_loc = ('css selector', 'form#theForm div:nth-of-type(15) div:nth-of-type(2) input')  # 提交订单按钮
    order_number_loc = ('css selector', 'div.flowBox h6 font')  # 生成订单后的订单号
    amount_payable_loc = ('css selector', 'div.flowBox table tbody tr td strong:nth-of-type(3)')  # 应付款金额
    user_center_loc = ('css selector', 'div.fl a')  # 用户中心
    my_order_loc = ('css selector', 'div.userMenu a:nth-of-type(3)')  # 我的订单
    my_order_number_loc = ('css selector', 'div.userCenterBox table tbody tr td a')  # 我的订单页面的订单号
    my_amount_payable_loc = ('css selector', 'div.userCenterBox table tbody tr td:nth-of-type(3)')  # 我的订单页面的订单总金额
    my_order_status_loc = ('css selector', 'div.userCenterBox table tbody tr td:nth-of-type(4)')  # 我的订单页面的应付款金额

    """封装操作层: 针对表现层中元素的操作"""

    def generate_paid_order(self, username, password):
        """
        生成已支付订单
        @param username: 用户名
        @param password: 密码
        @return:
        """
        self.click(self.buy_submit_loc)  # 点击立即购买按钮
        self.click(self.settlement_submit_loc)  # 点击去结算按钮
        self.send_keys(self.username_loc, username)  # 输入用户名
        self.send_keys(self.password_loc, password)  # 输入密码
        self.click(self.login_submit_loc)  # 点击登录按钮
        self.click(self.order_submit_loc)  # 点击提交订单按钮

    def generate_unpaid_order(self, username, password):
        """
        生成未支付订单
        @param username: 用户名
        @param password: 密码
        @return:
        """
        self.click(self.buy_submit_loc)  # 点击立即购买按钮
        self.click(self.settlement_submit_loc)  # 点击去结算按钮
        self.send_keys(self.username_loc, username)  # 输入用户名
        self.send_keys(self.password_loc, password)  # 输入密码
        self.click(self.login_submit_loc)  # 点击登录按钮
        self.click(self.payment_loc)  # 支付方式选择银行卡汇款/转账
        self.click(self.order_submit_loc)  # 点击提交订单按钮

    def get_order_number(self):
        """
        获取生成订单后的订单号
        @return:
        """
        return self.get_element_text(self.order_number_loc)

    def get_amount_payable(self):
        """
        获取生成订单后的应付款金额
        @return:
        """
        return self.get_element_text(self.amount_payable_loc)

    def into_my_order(self):
        """
        进入用户中心-我的订单
        @return:
        """
        self.click(self.user_center_loc)  # 点击用户中心
        self.click(self.my_order_loc)  # 点击订单管理

    def get_my_order_number(self):
        """
        获取我的订单页面的订单号
        @return:
        """
        return self.get_element_text(self.my_order_number_loc)

    def get_my_amount_payable(self):
        """
        获取我的订单页面的应付款金额
        @return:
        """
        return self.get_element_text(self.my_amount_payable_loc)

    def get_my_order_status(self):
        """
        获取我的订单页面的订单状态
        @return:
        """
        return self.get_element_text(self.my_order_status_loc)


if __name__ == '__main__':
    checkout = CheckoutPage(open_browser())  # 打开浏览器
    checkout.open_url(shopping_url)  # 进入商品详情页面
    checkout.generate_unpaid_order('Mike', '123456')  # 生成未支付订单
    print(checkout.get_order_number())  # 打印获取的订单号
    print(checkout.get_amount_payable())  # 打印获取的应付款金额
    checkout.into_my_order()  # 进入用户中心-我的订单
    print(checkout.get_my_order_number())  # 打印我的订单页面的订单号
    print(checkout.get_my_amount_payable())  # 打印我的订单页面的应付款金额
    print(checkout.get_my_order_status())  # 打印我的订单页面的订单状态
